package com.infore.maintenance.main.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.infore.common.model.entity.TBaseGroup;
import com.infore.maintenance.main.dao.TBaseGroupMapper;
import com.infore.maintenance.main.dao.TBaseQuerySqlMapper;
import com.infore.maintenance.main.service.BusinessExceptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.*;


@Slf4j
@Service
public class BusinessExceptServiceImpl extends ServiceImpl<TBaseGroupMapper, TBaseGroup> implements BusinessExceptService {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private TBaseQuerySqlMapper sqlMapper;

    @Override
    public void testExcept() {
//        String a= null;
//        System.out.println(a.toLowerCase());
//        String a="3f";
//        Integer.parseInt(a);
//        int a =3/0;
//        selectSql();


        TBaseGroup group1 = new TBaseGroup();
        group1.setGroupName("G1_xx");
        group1.setGroupType("MYSQL_xx");
//        insert(group1);

    }



    public void selectSql(){
//        String databaseType="MYSQL";
//        String sql = "select * from data_maintain.t_base_datasource where database_type=?";
        Page<Object> page = PageHelper.startPage(1, 1);
        String sql = "select RULE_ID as \"role_id\",CONTINUE_NUM as \"continue_num\",DATA_TYPE as \"data_type\",\n" +
                "       PROJECT_ID as \"project_id\",PROJECT_NAME as \"project_name\" from basic_db.t_basic_rule_detail_oth\n" +
                "where ( ${ruleId} is null or RULE_ID= ${ruleId}) and ( #{projectId} is null or PROJECT_ID= #{projectId})";


        String json = "{\"ruleId\":\"21468BB7034848FB89547663283483F1\",\"projectId\":\"186\"}";
        Map varMap= JSONUtil.parseObj(json);
//        Map<String, Object> varMap = new HashMap<>();
//        varMap.put("ruleId","21468BB7034848FB89547663283483F1");
//        varMap.put("projectId","186");
//        List<Object> parameters = new ArrayList<>();

//        sql = TextSqlNode.parseSql(sql,varMap,parameters);
//        RowMapper<Map<String, Object>> columnMapRowMapper= new ColumnMapRowMapper();
//        Object[] params = parameters.toArray();
//        List<Map<String, Object>> list = jdbcTemplate.query(sql, columnMapRowMapper, params);
/*        Map<String, Object> parameters = new HashMap<>();
        String executeSql =  TextSqlNode.parseSqlParamenters(sql,varMap,parameters);
        System.out.println(executeSql);
        System.out.println(JSONUtil.toJsonStr(parameters));

        List<Map<String, Object>> list = sqlMapper.queryBySqlAndParameters(executeSql, parameters);
        System.out.println(list.size());
        System.out.println(JSONUtil.toJsonStr(list));*/


        List<TBaseGroup> list = new ArrayList<>();
        TBaseGroup group1 = new TBaseGroup();
        group1.setGroupName("G1");
        group1.setGroupType("MYSQL");
        list.add(group1);

        TBaseGroup group2 = new TBaseGroup();
        group2.setGroupName("G2");
        group2.setGroupType("ORACLE");
        list.add(group2);
        String parametersJson = JSONUtil.toJsonStr(list);
        System.out.println(parametersJson);

//        String parametersJson = "[{\"groupName\":\"G1\",\"groupType\":\"MYSQL\"}," +
//                "{\"groupName\":\"G2\",\"groupType\":\"ORACLE\"}]";
        JSONArray jsonArray = JSONUtil.parseArray(parametersJson);
//        List<Map> parametersList = JSONUtil.toList(parametersJson, Map.class);



//        List<String> listHead = new ArrayList<>();
//        listHead.add("group_name");
//        listHead.add("group_type");

        Map<String, String> mapColumnProperty = new HashMap<>();
        mapColumnProperty.put("group_name","groupName");
        mapColumnProperty.put("group_type","groupType");

        List<TreeMap<String,Object>> dataList = new ArrayList<>();

        for (int i = 0; i < jsonArray.size(); i++) {
            TreeMap<String,Object> treeMap = new TreeMap<String,Object>();
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            for(Map.Entry<String,String>  entry:mapColumnProperty.entrySet()){
                String col = entry.getValue();
                Object colValue = jsonObject.get(col);
                treeMap.put(col,colValue);
            }
            dataList.add(treeMap);
        }

        Map<String, Object> batchList = new HashMap<>();
        batchList.put("tableName", "t_base_group");
        batchList.put("mapColumnProperty",mapColumnProperty);
        batchList.put("data",dataList);
        sqlMapper.insertTableListDynamicOfMysql(batchList);


    }
}
